package cn.jeefast.rest.service.impl;

import cn.jeefast.common.entity.MbDoctor;
import cn.jeefast.common.entity.MbResearch;
import cn.jeefast.common.entity.MbResearchData;
import cn.jeefast.common.entity.MbResearchStep;
import cn.jeefast.common.utils.R;
import cn.jeefast.rest.dao.*;
import cn.jeefast.rest.entity.TbUser;
import cn.jeefast.rest.service.MbDoctorService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author theodo
 * @since 2019-12-30
 */
@Service
public class MbDoctorServiceImpl extends ServiceImpl<MbDoctorDao, MbDoctor> implements MbDoctorService {

    @Autowired
    private MbDoctorDao doctorDao;
    @Autowired
    private MbResearchDao researchDao;
    @Autowired
    private MbResearchStepDao researchStepDao;
    @Autowired
    private MbResearchDataDao researchDataDao;
    @Autowired
    private TbUserDao tbUserDao;
    @Override
    public R homePage(Long docId) {
        //查询医生信息
//        MbDoctor mbDoctor = doctorDao.selectById(docId);
        TbUser mbDoctor = tbUserDao.selectById(docId);
        Map doctorInfo = new HashMap();
        doctorInfo.put("name",mbDoctor.getUsername());
        doctorInfo.put("company",mbDoctor.getCompany());
        //查询CKD的所有组别
        EntityWrapper ew = new EntityWrapper();
        ew.eq("proj_id",11);
        List<MbResearch> researchList = researchDao.selectList(ew);
        List<Map> reseList = new ArrayList<>();
        for (MbResearch mrs:researchList
             ) {
            Map m = new HashMap();
            m.put("resId",mrs.getResId());
            m.put("resName",mrs.getResName());
            m.put("projId",mrs.getProjId());
            //计算总数
//            EntityWrapper ew6 = new EntityWrapper();
//            ew6.eq("res_step_id",mrs.getResId());
//            ew6.eq("doc_id",docId);
//            List list = researchDataDao.selectList(ew6);
//            m.put("count",list.size());
            //根据研究组ID获取研究阶段集合
            EntityWrapper ew1 = new EntityWrapper();
            ew1.eq("res_id",mrs.getResId());
            List<MbResearchStep> list = researchStepDao.selectList(ew1);
            int count = 0 ;
            for (MbResearchStep mrs0:list) {
                EntityWrapper ew6 = new EntityWrapper();
                ew6.eq("res_step_id",mrs0.getStepId());
                ew6.eq("doc_id",docId);
                List reCount = researchDataDao.selectList(ew6);
                count += reCount.size();
            }
            m.put("count",count);
            reseList.add(m);
        }
        List result0 = new ArrayList();
        //查询这两个组别的相关信息
        for (MbResearch r:researchList) {
            //分别查询每一组关联的治疗阶段
            EntityWrapper ew1 = new EntityWrapper();
            ew1.eq("res_id",r.getResId());
            List<MbResearchStep> researchStepList = researchStepDao.selectList(ew1);
            //获取到阶段集合后，根据治疗阶段ID（RESID）获取每阶段的已完成、未完成、未通过的数据。
            List result = new ArrayList();
            for (MbResearchStep mrs:researchStepList) {
                Map m2 = new HashMap();
                EntityWrapper ew2 = new EntityWrapper();
                ew2.eq("res_step_id",mrs.getStepId());
                ew2.eq("state",3);
                ew2.eq("doc_id",docId);
                //已完成
                List list0 = researchDataDao.selectList(ew2);
                m2.put("finish",list0.size());
                //审核未通过
                EntityWrapper ew3 = new EntityWrapper();
                ew3.eq("res_step_id",mrs.getStepId());
                ew3.eq("state",2);
                ew3.eq("doc_id",docId);
                List list1 = researchDataDao.selectList(ew3);
                m2.put("un",list1.size());
                //未完成
                EntityWrapper ew4 = new EntityWrapper();
                ew4.eq("res_step_id",mrs.getStepId());
                ew4.eq("state",0);
                ew4.eq("doc_id",docId);
                List list2 = researchDataDao.selectList(ew4);
                EntityWrapper ew5 = new EntityWrapper();
                ew5.eq("res_step_id",mrs.getStepId());
                ew5.eq("state",1);
                ew5.eq("doc_id",docId);
                List list3 = researchDataDao.selectList(ew5);
                m2.put("unfinished",list2.size()+list3.size());
                m2.put("name",mrs.getStepName());
                result.add(m2);
            }
            result0.add(result);
        }
        Map map = new HashMap();
        map.put("doctorInfo",doctorInfo);
        map.put("researchList",reseList);
        map.put("result",result0);
        return R.ok("").put("data",map);
    }
}
